<?php
/** ***********************
 * 作者：Zpy
 * 日期：2015/5/21
 * 作用：数据模型
 ** ***********************/
class ticketModelHome extends coreFrameworkModel {
	function test(){
		//默认的数据库源
		$sql=SqlToolsClass::SelectItem("admin");
		$datas=$this->GetAll($sql);
		
		//切换数据库源
		/*$sql=SqlToolsClass::SelectItem("activities_company_bind");
		$datas=$this->GetRow($sql,"test");*/
		
		//切换回默认的数据源
		$sql=SqlToolsClass::SelectItem("admin");
		$datas=$this->GetAll($sql);
	}
	
	function getTicketInfo($number){
		$join = ' left join '.SqlToolsClass::getTableName('business').' b on b.id=t.shop_id';
		$sql = SqlToolsClass::SelectItem('ticket as t','t.number="'.$number.'"','t.*,b.business_name,b.taxpayerId,b.taxpayerKey',$join);
		return $this->GetRow($sql);
	}
	function updateTicket($number,$data){
		$sql=SqlToolsClass::EditData("ticket", $data,"number=".$number);
		return $this->Execute($sql);
	}
	//获取商户应用key
	function getBusinessAppKey($taxpayerId)
	{
		$join = ' left join '.SqlToolsClass::getTableName('channel').' c on c.id=b.channel_id'.' left join '.SqlToolsClass::getTableName('channel_app').' ca on ca.channel_id=c.id';
		$sql = SqlToolsClass::SelectItem('business as b','b.taxpayerId="'.$taxpayerId.'"','ca.app_key',$join);
		return $this->GetAll($sql);
	}
	//验证appkey真实性
	function getAppKeyInfo($appkey){
		$sql = SqlToolsClass::SelectItem('channel_app','app_key="'.$appkey.'"');
		return $this->GetRow($sql);
	}
	//插入日志
	function insertApiLog($data){
		$sql = SqlToolsClass::InsertData('api_log',$data);
		return $this->Execute($sql);
	}
	//插入发票查询信息
	function saveTicketQuery($data){
		$sql = SqlToolsClass::SelectItem('ticket_query','requestCode="'.$data['requestCode'].'"');
		
		$query_info = $this->GetRow($sql);
		
		
		if (!$query_info) {
			$sql = SqlToolsClass::InsertData('ticket_query',$data);
			return $this->Execute($sql);
		}else{
			$sql = SqlToolsClass::EditData('ticket_query',$data,'id='.$query_info['id']);
			$res = $this->Execute($sql);
			if ($res) {
				return $query_info['id'];
			}else{
				return false;
			}
			
		}
	}
	//保存发票图片
	function saveTicketQueryImages($datas){
		$sql = SqlToolsClass::DeleteData('ticket_query_images','q_id='.$q_id);
		$this->Execute($sql);
		$sql = SqlToolsClass::InsertDatas('ticket_query_images',$datas);
		return $this->Execute($sql);
		
	}
	//更新发票状态
	function updataTicket($data,$where){
		$where = implode(' and ',$where);
		$sql = SqlToolsClass::EditData('ticket',$data,$where);
		
		return $this->Execute($sql);
		
	}
	//获取商户信息
	function getBusinessInfo($businesskey){
		$sql = SqlToolsClass::SelectItem('business','business_key="'.$businesskey.'"');
		return $this->GetRow($sql);
	}
	//获取商品信息
	function getTicketGoods($ticket_id){
		$sql = SqlToolsClass::SelectItem('ticket_goods','ticket_id='.$ticket_id);
		return $this->GetAll($sql);
		
	}
	//发票查询详细信息
	function getTicketQuery($requestCode){
		$sql = SqlToolsClass::SelectItem('ticket_query','requestCode="'.$requestCode.'"');
		return $this->GetRow($sql);
	}
	//发票图片
	function getTicketQueryImages($query_id){
		$sql = SqlToolsClass::SelectItem('ticket_query_images','q_id="'.$query_id.'"');
		return $this->GetAll($sql);
	}
	//创建待开发票
	function createTicket($data){
		$sql = SqlToolsClass::InsertData('ticket',$data);
		return $this->Execute($sql);
	}
	//增加商品信息
	function insertTicketGoods($datas){
		$sql = SqlToolsClass::InsertDatas('ticket_goods',$datas);
		return $this->Execute($sql);
	}
	
}
?>